var VGroupModalDemo = angular.module('FusionStorUI.vgroup', []);
VGroupModalDemo.controller('VGroupModalCtrl', ['$scope','$modal', '$http', function($scope, $modal, $http) {
    var modalInstance;
    $scope.templateData = '';
    $scope.open = function(url) {  
        $scope.item = {};
        $http.get(url).success(function(data){
            $scope.templateData = data;
            modalInstance = $modal.open({  
                template : $scope.templateData,
                controller : VGroupModalInstanceCtrl,  
                backdrop:'static',  
                keyboard: false,
                resolve : { 
                    item : function() {  
                        return $scope.item;  
                    }  
                }  
            });  
        });  
    };  

    $scope.close = function() {
        modalInstance.dismiss('cancel');  
    }
}]); 
var VGroupModalInstanceCtrl = function($scope, $modalInstance, item) {  
    $scope.item = item;  
    $scope.selected = {  
        item : $scope.item 
    };  
    $scope.ok = function() {  
        submit_vgroup_create();
    };  
    $scope.update = function(){  
        submit_vgroup_update();
    }; 
    $scope.vgroup_policy_create = function() {
        submit_vgroup_policy();
    };
    $scope.cancel = function() {  
        $modalInstance.dismiss('cancel');  
    };  
};  


function get_vgroup_volume_save(url){
    var result = {
        datatype: 'json',
        url: "/grid_volume_for_cgsnap?vgroup_id=-1",
        mtype: 'GET',
        colModel :[
            {label:"编号",name:'id', index:'id', width:30, align:'',hidden:true},
            {label:"路径",name:'name', index:'name', width:200, align:'',
                formatter: function(cellvalue, option, row) {
                    var outerWidth = $('#volume_save_name').width() - 20;
                    return '<span style="display:inline-block;text-overflow:ellipsis; white-space:nowrap;overflow:hidden;width:' + outerWidth + 'px;" href="#">' + cellvalue + '</span>';
                },
                unformat: function(cellvalue) {
                    return cellvalue;
                }
            },
            {label:"移除组",name:'choose', index:'choose', width:80, align:''},
        ],
        rowNum:1000000,
        sortname:'id',
        sortorder:'desc',
        viewrecords: true,
        height:300,
        autowidth:true,
        gridComplete:function(){
            var ids=$(this).jqGrid('getDataIDs');
            for(var i=0; i<ids.length; i++){ 
                volume_id = $(this).getCell(ids[i], "id");
                var choose = '<span class="minus_sign" onclick=volume_del_group('+volume_id+')>'+'&nbsp;'+'</span>'
                $(this).jqGrid('setRowData', ids[i], {choose: choose}); 
            }
        },
    }
    if (typeof(url) != 'undefined') {
        result['url'] = url;
    } 
    return result
}
function get_vgroup_volume_choose(url){
    var result = {
        datatype: 'json',
        mtype: 'GET',
        colModel :[
            {label:"编号",name:'id', index:'id', width:30, align:'',hidden:true},
            {label:"路径",name:'name', index:'name', width:200, align:'',
            formatter: function(cellvalue, option, row) {
            var outerWidth = $('#volume_choose_name').width() - 20;
            return '<span style="display:inline-block;text-overflow:ellipsis; white-space:nowrap;overflow:hidden;width:' + outerWidth + 'px;" href="#">' + cellvalue + '</span>';
        },
        unformat: function(cellvalue) {
            return cellvalue;
        }},
            {label:"加入组",name:'choose', index:'choose', width:80, align:''},
        ],
        rowNum:100000,
        sortname:'id',
        sortorder:'desc',
        viewrecords: true,
        height:300,
        autowidth:true,
        gridComplete:function(){
            var ids=$(this).jqGrid('getDataIDs');
            for(var i=0; i<ids.length; i++){ 
                volume_id = $(this).getCell(ids[i], "id");
                    choose = '<span class="host_add_group" onclick=volume_add_group('+volume_id+')>'+'&nbsp;'+'</span>'
                    $(this).jqGrid('setRowData', ids[i], {choose: choose}); 
                }   
            },
        }
        if (typeof(url) != 'undefined') {
            result['url'] = url;
        } else {
            result['url'] = '/grid_volume_for_cgsnap';
        }
        return result
    }

    function volume_add_group(volume_id){
        var rowdata = $("#volume_choose").jqGrid('getRowData', volume_id);
        var choose = '<span class="minus_sign" onclick=volume_del_group('+volume_id+')>'+'&nbsp;'+'</span>'
        rowdata.choose = choose;
        $("#volume_save").addRowData(volume_id, rowdata, 'first');
        $("#volume_choose").jqGrid('delRowData', volume_id);
    }

    function volume_del_group(volume_id) {
        var rowdata = $("#volume_save").jqGrid('getRowData', volume_id);
        var choose = '<span class="minus_sign" onclick=volume_add_group('+volume_id+')>'+'&nbsp;'+'</span>'
        rowdata.choose = choose;
        $("#volume_choose").addRowData(volume_id, rowdata, 'first');
        $("#volume_save").jqGrid('delRowData', volume_id);
    }


